<?php
function showSelector($team_id, $race_id) {
    global $ruleSets, $season, $races;
    echo<<< EOQ
	<tr><td colspan="2">
	Change Rule Set and/or Race
    <form name="classic" method="get" action="team_creator.php">
EOQ;
    if (strlen($team_id) > 0) {
        echo<<< EOQ
        <input type="hidden" name="team_id" value="$team_id" />
EOQ;
    }
    $idx = 0;
    $ruleIdParam = getParam("ruleSet");
    $defaultRuleSet = 0;
    $checkedRuleSetId = 1;
    if (isset ($ruleIdParam)) {
        $checkedRuleSetId = $ruleIdParam;
    } else {
        if (isset ($team_id) && $season->restrictedRuleSets()) {
            $allowedRuleSets = $season->getAllowedRuleSetIds();
            $checkedRuleSetId = $allowedRuleSets[0];
        }
    }

    // if the defaultRuleSet is set then that should be checked
    // else if the team id is present the first valid rule set should be checked
    foreach ($ruleSets as $ruleSet) {
	    $checked = "";
        $ruleId = $ruleSet->getId();
        $ruleShortName = $ruleSet->getShortName();
        $title = "Show races from " . $ruleShortName;
        if (isset ($team_id) && $season->restrictedRuleSets()) {
            $checked = "disabled";
            $title = "Not allowed for this season";
            foreach ($season->getAllowedRuleSetIds() as $seasonRuleId) {
                if ($seasonRuleId == $ruleId) {
				    $checked = "";
                    $title = "Show races from " . $ruleShortName;
                    break;
                }
            }
        }
        if ($checkedRuleSetId == $ruleId) {
            $defaultRuleSet = $idx;
            $checked = "checked";
        }
        echo<<< EOQ
	<span title="$title"><input type="radio" name="ruleSet" value="$ruleId" $checked onChange="updatecities('$idx')" /> $ruleShortName </span><br />
EOQ;
        $idx++;
    }
    $size = count($ruleSets);
    echo<<< EOQ
</td><td colspan="3">
<select name="race_id" size="$size" style="width: 135px; height: 100px;" onClick="submit(this.options[this.options.selectedIndex].value)">
</select>
</form>
</tr></td>
<script type="text/javascript">

var countrieslist=document.classic.countries
var citieslist=document.classic.race_id

var cities=new Array()

EOQ;
    $rsDao = new RuleSetDao();
    $idx = 0;
    foreach ($ruleSets as $ruleSet) {
        $raceIds = $rsDao->findRaceIdsForRuleSet($ruleSet);
        echo "cities[$idx]=[";
        $firstRace = true;
        foreach ($raceIds as $raceId) {
            $race = getRace($raceId);
            if (!$firstRace) {
                echo ", ";
            } else {
                $firstRace = false;
            }
            echo "\"" . $race->name . "|" . $race->id . "\"";
        }
        echo "]\n";
        $idx++;
    }

    echo<<< EOQ

function updatecities(selectedcitygroup){
	citieslist.options.length=0
	for (i=0; i<cities[selectedcitygroup].length; i++)
		citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
}

function submit(race_id) {
    document.classic.race_id=race_id;
    document.classic.submit();
}

updatecities($defaultRuleSet);
</script>    
EOQ;
}

$noSeasonChange = true;
$showRaceLinks = true;

include_once ("includes/init.php");

$race_id = getParam("race_id");
if (!isset ($race_id)) {
    $race_id = 1;
}
$race = getRace($race_id);
$raceText = $race->getRaceDisplay();
$title = $raceText . " details";

// Try to update team
$team_id = getParam("team_id");
if (strlen($team_id) > 0) {
    $team = $teams->getTeam($team_id);
}

$goed = getParam("goed");
if (isset ($goed) and isset ($loggedin) and $loggedin->canUpdateTeam($team) and !$team->usesTeamEditor()) {
    $rs = executeQuery("select count(1) from players where fk_team_id=$team_id");
    $row = mysql_fetch_row($rs);

    if ($row[0] == 0) {
        $numPositions = sizeof($race->positions);
        $i = 1;
        $player_id = 0;
        $roster_num = 1;
        foreach ($race->positions as $position) {
            if ($position->isJourneyman()) {
                continue;
            }
            $string = "qty" . $i;
            $quantity = $_POST[$string];
            for ($j = 0; $j < $quantity; $j++) {
                $team->updatePlayer(0, $roster_num++, "", $position->position, $position->ma, $position->st, $position->ag, $position->av, $position->skillsStr, false, 0, 0, 0, 0, 0, 0, 0, $position->cost);
            }
            $i++;
        }
    }
    $string = "qty" . $i++;
    $apoth = $_POST[$string];
    $string = "qty" . $i++;
    $fanFactor = $_POST[$string];
    $string = "qty" . $i++;
    $rerolls = $_POST[$string];
    $teamTotal = getParam("teamTotal");
    $treasury = 0;
    if ($teamTotal < 1000) {
        $treasury = 1000 - $teamTotal;
    }

    $error = $teams->updateFullTeam($team, $team->name, $race, $treasury, 0, 0, $rerolls, $race->rerollCost, $fanFactor, 0, 0, $apoth, "#ffffff", "#aa0000", "#000000", "#ffffff", "#c08040", "#400000");

    $team = $teams->getTeam($team_id);
    $team->save();

    $redirect = "team_editor.php?reload=true&id=$team_id";
    $redirectStr = "your team";
    include ("includes/redirect.php");
    return;
}

$posNum = 1;
$numPositions = 0;
foreach ($race->positions as $position) {
    if ($position->isJourneyman()) {
        continue;
    }
    $numPositions++;
}

$scriptHead = "<script language=\"javascript\">

    function updateTotals(rownum)
    {
        var total = 0;
        var exeLine='total=eval(document.teamform.cost'+rownum+'.value)'
        eval (exeLine)
        var exeLine='total*=eval(document.teamform.qty'+rownum+'.value)'
        eval (exeLine)
        var exeLine='document.teamform.total'+rownum+'.value=total'
        eval (exeLine)

        updateQuantities();
        updateAll();
    }

    function changeFocus(rowNum)
    {
        var exeLine='document.teamform.qty' + rowNum + '.focus()'
        eval (exeLine);
        var exeLine='document.teamform.qty' + rowNum + '.select()'
        eval (exeLine);
    }

    function updateQuantities()
    {
      var total = 0;";
for ($i = 1; $i <= $numPositions; $i++) {
    $scriptHead = $scriptHead . "
                                    var exeLine='total+=eval(document.teamform.qty$i.value)';
                                    eval (exeLine);";
}
$scriptHead = $scriptHead . "
      var exeLine='document.teamform.teamQty.value=total'
      eval (exeLine)
    }

    function updateAll()
    {
      var total = 0;";
for ($i = 1; $i < $numPositions +4; $i++) {
    $scriptHead = $scriptHead . "
                                    var exeLine='total+=eval(document.teamform.total$i.value)';
                                    eval (exeLine);";
}
$scriptHead = $scriptHead . "
      var exeLine='document.teamform.teamTotal.value=total'
      eval (exeLine)
    }
    </script>";

$noSeasonChange = true;
include ("includes/header.php");

echo<<< EOQ
    <caption><h1>$raceText</h1></caption>
EOQ;
showSelector($team_id, $race_id);
$long = $ruleSets[$race->getRuleId()]->getLongName();
echo<<< EOQ
    <tr>
        <th>Max Qty</th><th>Position</th><th>Cost</th><th>Quantity</th><th>Total</th>
    </tr>
    <form name="teamform" method="post">
        <input type="hidden" name="race_id" value="$race_id" />
        <input type="hidden" name="team_id" value="$team_id" />
EOQ;
$i = 1;
$j = 2;
foreach ($race->positions as $position) {
    if ($position->isJourneyman()) {
        continue;
    }

    echo getEchoTR($i);

    echo<<< EOQ
        <td>$position->qty</td>
        <td><span title="$position->ma $position->st $position->ag $position->av $position->skillsStr $position->skillGroupsStr">
EOQ;
    if ($position->isStar()) {
        echo<<< EOQ
        <i>$position->position</i>
EOQ;
    } else {
        echo<<< EOQ
        $position->position
EOQ;
    }
    echo<<< EOQ
</span></td>    
        <td align="center"><input name="cost$i" type="text" size="3" value="$position->cost" onFocus="changeFocus($i);"/></td>
        <td align="center"><input name="qty$i" type="text" size="2" value="0" onChange="updateTotals('$i')"/></td>
        <td align="center"><input name="total$i" type="text" size="3" value="0" onFocus="changeFocus($j);"/></td>
    </tr>
EOQ;
    $i++;
    $j++;
}
echo getEchoTR($i);
echo<<< EOQ
        <td>1</td>
        <td>Apothecary</td>
        <td align="center"><input name="cost$i" type="text" size="3" value="50" onFocus="changeFocus($i);"/></td>
        <td align="center"><input name="qty$i" type="text" size="2" value="0" onChange="updateTotals('$i')"/></td>
        <td align="center"><input name="total$i" type="text" size="3" value="0" onFocus="changeFocus($j);"/></td>
    </tr>
EOQ;
$i++;
$j++;
echo getEchoTR($i);
echo<<< EOQ
        <td>0-9</td>
        <td>Fan Factor</td>
        <td align="center"><input name="cost$i" type="text" size="3" value="10" onFocus="changeFocus($i);"/></td>
        <td align="center"><input name="qty$i" type="text" size="2" value="0" onChange="updateTotals('$i')"/></td>
        <td align="center"><input name="total$i" type="text" size="3" value="0" onFocus="changeFocus($j);"/></td>
    </tr>
EOQ;
$i++;
$j++;
echo getEchoTR($i);
echo<<< EOQ
        <td>&nbsp;</td>
        <td>Rerolls</td>
        <td align="center"><input name="cost$i" type="text" size="3" value="$race->rerollCost" onFocus="changeFocus($i);"/></td>
        <td align="center"><input name="qty$i" type="text" size="2" value="0" onChange="updateTotals('$i')"/></td>
        <td align="center"><input name="total$i" type="text" size="3" value="0" onFocus="changeFocus(1);"/></td>
    </tr>
EOQ;
$i++;
$j++;
echo getEchoTR($i);
echo<<< EOQ
        <th colspan="3">&nbsp;</th>
        <th># Players</th>
        <th>Total<br />Cost</th>
    </tr>
EOQ;
echo getEchoTR(++ $j);
echo<<< EOQ
        <td colspan="3">&nbsp;</td>
        <td align="center"><input name="teamQty" type="text" size="2" value="0" onFocus="changeFocus(1);"/></td>
        <td align="center"><input name="teamTotal" type="text" size="4" value="0" onFocus="changeFocus(1);"/></td>
    </tr>
EOQ;
echo getEchoTR(++ $j);
echo<<< EOQ
        <td colspan="5" align="center">$long</td>
    </tr>
EOQ;
echo getEchoTR(++ $j);
echo<<< EOQ
        <td colspan="5">$race->comments</td>
    </tr>
EOQ;
if (strlen($team_id) > 0) {
    echo getEchoTR(++ $j);
    echo<<< EOQ
        <td colspan="5" align="center">
            <input type="submit" name="goed" value="Go to Editor">
        </td>
EOQ;
}
echo<<< EOQ
    </form>
EOQ;

include ("includes/footer.php");
?>